/*
 * Copyright 2011 Michele Mancioppi [michele.mancioppi@gmail.com]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cave.nice.testMessage.data;

import java.util.List;
import java.util.NoSuchElementException;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import cave.nice.testMessage.data.ReportingPolicy.ReportingPolicyType;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
@Entity
public class VerifiedAccount extends Account {

	@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List<Test> tests;

	@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List<ReportingPolicy> reportingPolicies;

	public VerifiedAccount() {}

	public List<Test> getTests() {
		return tests;
	}

	public void setTests(List<Test> openTests) {
		this.tests = openTests;
	}

	public ReportingPolicy getReportingPolicy(final ReportingPolicyType type) throws NoSuchElementException {
		return Iterables.find(getReportingPolicies(), new Predicate<ReportingPolicy>() {

			@Override
			public boolean apply(ReportingPolicy reportingPolicy) {
				return type.equals(reportingPolicy.getType());
			}

		});
	}

	public List<ReportingPolicy> getReportingPolicies() {
		return reportingPolicies;
	}

	public void setReportingPolicies(List<ReportingPolicy> reportingPolicies) {
		this.reportingPolicies = reportingPolicies;
	}

}
